#import "IDCRViewController.h"

@interface IDCRViewController ()

@end

@implementation IDCRViewController
@synthesize stopButton;
@synthesize score;
@synthesize position;
@synthesize startButton;
@synthesize paddle;
@synthesize penguin;
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    [stopButton setHidden:YES];
    velocity = 0;
    touchX = penguin.center.x;
    [NSTimer scheduledTimerWithTimeInterval:(0.01) target:(self) selector:@selector (moveBall) userInfo:nil repeats:(YES)];
    [NSTimer scheduledTimerWithTimeInterval:(0.01) target:(self) selector:@selector (movePaddle) userInfo:nil repeats:(YES)];
    
    for(int i = 0; i < 10; i++){
        UIImageView *brick = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"brick.png"]];
        [brick setFrame: CGRectMake(i%5*(60), i/5*60, 50, 50)];
        [self.view addSubview: brick];
        [brickArray addObject: brick];
     }
	// Do any additional setup after loading the view, typically from a nib.
} 

- (void)viewDidUnload
{
    [self setPaddle:nil];
    [self setPenguin:nil];
    [self setPosition:nil];
    [self setStartButton:nil];
    [self setStopButton:nil];
    [self setScore:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
-(void) movePaddle
{
    if(paddle.center.x > touchX){
        [paddle setCenter:CGPointMake(paddle.center.x-velocity, paddle.center.y)];
    }else if(paddle.center.x < touchX){
        [paddle setCenter:CGPointMake(paddle.center.x+velocity, paddle.center.y)];
    }
    
    if(paddle.center.x - touchX<1 && paddle.center.x -touchX > -1){
        [paddle setCenter:CGPointMake(touchX, paddle.center.y)];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    touchX = touchLocation.x;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void) moveBall
{
    if(penguin.center.x >= 280){
        bounce = 1;
    }else if(penguin.center.x <= 20){
        bounce = 0;
    }
    
    switch(bounce){
        case 0:
            [penguin setCenter:CGPointMake(penguin.center.x+velocity, penguin.center.y)];
            penguin.transform = CGAffineTransformMakeScale(1, 1);
            break;
        case 1:
            [penguin setCenter:CGPointMake(penguin.center.x-velocity, penguin.center.y)];
            penguin.transform = CGAffineTransformMakeScale(-1, 1);
            break;
    }
    
    if(CGRectIntersectsRect(penguin.frame, paddle.frame)){
        bounce2 = 1;
        intScore ++;
    }else if(penguin.center.y <= 30){
        bounce2 = 0;
    }
    switch(bounce2){
        case 0:
            [penguin setCenter:CGPointMake(penguin.center.x, penguin.center.y+velocity)];
            break;
        case 1:
            [penguin setCenter:CGPointMake(penguin.center.x, penguin.center.y-velocity)];
            break;
    }
    [position setText:[NSString stringWithFormat:@"(%0.2f, %0.2f)", penguin.center.x, penguin.center.y]];
    [score setText:[NSString stringWithFormat:@"%.i", intScore]];
    
    
}

- (IBAction)startBtn:(id)sender {
    bounce = 0;
    velocity = 1;
     [startButton setHidden:YES];
    [stopButton setHidden:NO];
}

- (IBAction)stopBtn:(id)sender {    
    velocity = 0;
    [penguin setCenter:CGPointMake(150,204)];
    [startButton setHidden:NO];
    [stopButton setHidden:YES];
}

@end
